cd /Users/Laura/Documents/Research/PDHosp/Replication
log using WMKM_REStat_hospital_regs.log, replace
/************************************************************
PROGRAM: WMKM_REStat_hospital_regs.do
PURPOSE: RD analysis of hospital and ED data for 
		 Wherry, Miller, Kaestner, and Meyer
DATE: 	 February 7, 2017
NOTES:	 To run, (1) change directory above to a local directory. 
		 (2) Create subfolders called src, data, and output. 
		 (3) Put source hospital and ED data files in src directory. 
CONTACT: Sarah Miller, smille@umich.edu
		 Laura Wherry, lwherry@mednet.ucla.edu
*************************************************************/
set more off

/************************************************************
A) 2009 Hospital data, all races
*************************************************************/
clear
insheet using src/allraces2009.csv

sort byear bmonth
egen x_c=group(byear bmonth)
replace x_c=x_c-49
gen post=1*(x_c>=0)
gen x_c2=x_c^2

**chronic/nonchronic
gen logchron=log(hcupchronic)
gen lognonchron=log(count-preg-hcupchronic)

gen nonchron=count-preg-hcupchronic
gen nonpreg=count-preg

**for placebo tests
gen loga=log(appendicitis)
gen logi=log(injury)

***costs
gen logcost=log(cost)

***all non pregnancy hospitalizations + alternative specifications
global main lognp logchron lognonchron loga logi
global levs nonpreg hcupchronic nonchron
global costs logcost 

!rm output/diagcodes2009*
!rm output/main2009*
!rm output/costs*
!rm output/levels2009*

* Appendix Table 3 and 21, all races
foreach x of var $main {
	reg `x' post post#(c.x_c c.x_c2) i.bmonth, r
	outreg2 using output/main2009, ci bdec(3) ctitle("`x'", "all races", "4 yrs") append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 1297 & x_c !=36, r
	outreg2 using output/main2009, ci bdec(3) ctitle("`x'", "all races", "3 yrs") append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 577 & x_c !=24 , r
	outreg2 using output/main2009, ci bdec(3) ctitle("`x'", "all races", "2 yrs") append excel

	regress `x' i.bmonth, robust 
	predict y`x', xb
	gen e`x'=`x'-y`x'
		
	rdrobust e`x' x_c, bwselect(IK)
	outreg2 using output/main2009, ci bdec(3)  ctitle("`x'", "all races", "IK") append excel
	rdrobust e`x' x_c, bwselect(CCT)
	outreg2 using output/main2009, ci bdec(3)  ctitle("`x'", "all races", "CCT") append excel
	drop y`x' e`x'
}
*Appendix Table 23, all races
foreach x of var $costs {
	reg `x' post post#(c.x_c c.x_c2) i.bmonth, r
	outreg2 using output/costs, ci bdec(3) ctitle("`x'", "all races", "4 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 1297 & x_c !=36, r
	outreg2 using output/costs, ci bdec(3) ctitle("`x'", "all races", "3 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 577 & x_c !=24 , r
	outreg2 using output/costs, ci bdec(3) ctitle("`x'", "all races", "2 yrs")append excel

	regress `x' i.bmonth, robust 
	predict y`x', xb
	gen e`x'=`x'-y`x'
	
	rdrobust e`x' x_c, bwselect(IK)
	outreg2 using output/costs, ci bdec(3)  ctitle("`x'", "all races", "IK") append excel
	rdrobust e`x' x_c, bwselect(CCT)
	outreg2 using output/costs, ci bdec(3)  ctitle("`x'", "all races", "CCT") append excel
	drop y`x' e`x'
}
*Appendix Table 25, all races
foreach x of var $levs {
	reg `x' post post#(c.x_c c.x_c2) i.bmonth, r
	outreg2 using output/levels2009, ci bdec(3) ctitle("`x'", "all races", "4 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 1297 & x_c !=36, r
	outreg2 using output/levels2009, ci bdec(3) ctitle("`x'", "all races", "3 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 577 & x_c !=24 , r
	outreg2 using output/levels2009, ci bdec(3) ctitle("`x'", "all races", "2 yrs")append excel

	regress `x' i.bmonth, robust 
	predict y`x', xb
	gen e`x'=`x'-y`x'
		
	rdrobust e`x' x_c, bwselect(IK)
	outreg2 using output/levels2009, ci bdec(3)  ctitle("`x'", "all races", "IK") append excel
	rdrobust e`x' x_c, bwselect(CCT)
	outreg2 using output/levels2009, ci bdec(3)  ctitle("`x'", "all races", "CCT") append excel
	drop y`x' e`x'
}

/************************************************************
B) 2009 Hospital data, blacks
*************************************************************/
clear
insheet using src/black2009.csv

sort byear bmonth
egen x_c=group(byear bmonth)
replace x_c=x_c-49
gen post=1*(x_c>=0)
gen x_c2=x_c^2

**chronic/nonchronic
gen logchron=log(hcupchronic)
gen lognonchron=log(count-preg-hcupchronic)

gen nonchron=count-preg-hcupchronic
gen nonpreg=count-preg

**for placebo tests
gen loga=log(appendicitis)
gen logi=log(injury)

**By DX code
gen loginfect=log(infect)
gen logneoplams=log(neoplasms)
gen logendometa=log(endometa)
gen logblood=log(blood)
gen logmentaldisorders=log(mentaldisorders)
gen lognervoussys=log(nervoussys)
gen logsense=log(sense)
gen logcirculatory=log(circulatory)
gen logrespiratory=log(respiratory)
gen logdigestive=log(digestive)
gen loggenitourinary=log(genitourinary)
gen logskin=log(skin)
gen logmuscle=log(muscle)
gen logcongenital=log(congenital)
gen logilldefined=log(illdefined)

summ infect neoplasms endometa blood mentaldisorders nervoussys sense ///
circulatory respiratory digestive genitourinary skin muscle congenital illdefined

***costs
gen logcost=log(cost)
gen logpubc=log(publicc)
gen logselfc=log(selfc)
gen logprivc=log(privatec)
gen logfreec=log(freecarec)

***all non pregnancy hospitalizations + alternative specifications
global outcomes loginfect logneoplams logendometa logblood logmentaldisorders ///
lognervoussys logsense logcirculatory logrespiratory logdigestive ///
loggenitourinary logskin logmuscle logcongenital logilldefined
global main lognp logchron lognonchron loga logi 
global levs nonpreg hcupchronic nonchron
global costs logcost logpubc logselfc logprivc logfreec

*Appendix Table 5
foreach x of var $outcomes {
	reg `x' post post#(c.x_c c.x_c2) i.bmonth, r
	outreg2 using output/diagcodes2009, ci bdec(3) ctitle("`x'", "blacks", "4 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 1297 & x_c !=36, r
	outreg2 using output/diagcodes2009, ci bdec(3) ctitle("`x'", "blacks", "3 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 577 & x_c !=24 , r
	outreg2 using output/diagcodes2009, ci bdec(3) ctitle("`x'", "blacks", "2 yrs")append excel

	regress `x' i.bmonth, robust 
	predict y`x', xb
	gen e`x'=`x'-y`x'
	
	rdrobust e`x' x_c, bwselect(IK)
	outreg2 using output/diagcodes2009, ci bdec(3)  ctitle("`x'", "blacks", "IK") append excel
	rdrobust e`x' x_c, bwselect(CCT)
	outreg2 using output/diagcodes2009, ci bdec(3)  ctitle("`x'", "blacks", "CCT") append excel
	drop y`x' e`x'
}
*Table 4 and Appendix Table 21, blacks
foreach x of var $main {
	reg `x' post post#(c.x_c c.x_c2) i.bmonth, r
	outreg2 using output/main2009, ci bdec(3) ctitle("`x'", "blacks", "4 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 1297 & x_c !=36, r
	outreg2 using output/main2009, ci bdec(3) ctitle("`x'", "blacks", "3 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 577 & x_c !=24 , r
	outreg2 using output/main2009, ci bdec(3) ctitle("`x'", "blacks", "2 yrs")append excel

	regress `x' i.bmonth, robust 
	predict y`x', xb
	gen e`x'=`x'-y`x'
		
	rdrobust e`x' x_c, bwselect(IK)
	outreg2 using output/main2009, ci bdec(3)  ctitle("`x'", "blacks", "IK") append excel
	rdrobust e`x' x_c, bwselect(CCT)
	outreg2 using output/main2009, ci bdec(3)  ctitle("`x'", "blacks", "CCT") append excel
	drop y`x' e`x'
}
*Appendix Tables 23-24, blacks
foreach x of var $costs {
	reg `x' post post#(c.x_c c.x_c2) i.bmonth, r
	outreg2 using output/costs, ci bdec(3) ctitle("`x'", "blacks", "4 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 1297 & x_c !=36, r
	outreg2 using output/costs, ci bdec(3) ctitle("`x'", "blacks", "3 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 577 & x_c !=24 , r
	outreg2 using output/costs, ci bdec(3) ctitle("`x'", "blacks", "2 yrs")append excel

	regress `x' i.bmonth, robust 
	predict y`x', xb
	gen e`x'=`x'-y`x'
		
	rdrobust e`x' x_c, bwselect(IK)
	outreg2 using output/costs, ci bdec(3)  ctitle("`x'", "blacks", "IK") append excel
	rdrobust e`x' x_c, bwselect(CCT)
	outreg2 using output/costs, ci bdec(3)  ctitle("`x'", "blacks", "CCT") append excel
	drop y`x' e`x'
}
*Appendix Table 25, blacks
foreach x of var $levs {
	reg `x' post post#(c.x_c c.x_c2) i.bmonth, r
	outreg2 using output/levels2009, ci bdec(3) ctitle("`x'", "blacks", "4 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 1297 & x_c !=36, r
	outreg2 using output/levels2009, ci bdec(3) ctitle("`x'", "blacks", "3 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 577 & x_c !=24 , r
	outreg2 using output/levels2009, ci bdec(3) ctitle("`x'", "blacks", "2 yrs")append excel

	regress `x' i.bmonth, robust 
	predict y`x', xb
	gen e`x'=`x'-y`x'
		
	rdrobust e`x' x_c, bwselect(IK)
	outreg2 using output/levels2009, ci bdec(3)  ctitle("`x'", "blacks", "IK") append excel
	rdrobust e`x' x_c, bwselect(CCT)
	outreg2 using output/levels2009, ci bdec(3)  ctitle("`x'", "blacks", "CCT") append excel
	drop y`x' e`x'
}

/************************************************************
C) 2009 Hospital data, nonblacks
*************************************************************/
clear
insheet using src/nonblack2009.csv

sort byear bmonth
egen x_c=group(byear bmonth)
replace x_c=x_c-49
gen post=1*(x_c>=0)
gen x_c2=x_c^2

**chronic/nonchronic
gen logchron=log(hcupchronic)
gen lognonchron=log(count-preg-hcupchronic)

gen nonchron=count-preg-hcupchronic
gen nonpreg=count-preg

**for placebo tests
gen loga=log(appendicitis)
gen logi=log(injury)

***costs
gen logcost=log(cost)

***all non pregnancy hospitalizations + alternative specifications
global main lognp logchron lognonchron loga logi 
global levs nonpreg hcupchronic nonchron
global costs logcost 

*Table 4 and Appendix Table 21, nonblacks
foreach x of var $main {
	reg `x' post post#(c.x_c c.x_c2) i.bmonth, r
	outreg2 using output/main2009, ci bdec(3) ctitle("`x'", "non-blacks", "4 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 1297 & x_c !=36, r
	outreg2 using output/main2009, ci bdec(3) ctitle("`x'", "non-blacks", "3 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 577 & x_c !=24 , r
	outreg2 using output/main2009, ci bdec(3) ctitle("`x'", "non-blacks", "2 yrs")append excel

	regress `x' i.bmonth, robust 
	predict y`x', xb
	gen e`x'=`x'-y`x'
		
	rdrobust e`x' x_c, bwselect(IK)
	outreg2 using output/main2009, ci bdec(3)  ctitle("`x'", "non-blacks", "IK") append excel
	rdrobust e`x' x_c, bwselect(CCT)
	outreg2 using output/main2009, ci bdec(3)  ctitle("`x'", "non-blacks", "CCT") append excel
	drop y`x' e`x'
}

*Appendix Table 25, nonblacks
foreach x of var $levs {
	reg `x' post post#(c.x_c c.x_c2) i.bmonth, r
	outreg2 using output/levels2009, ci bdec(3) ctitle("`x'", "non-blacks", "4 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 1297 & x_c !=36, r
	outreg2 using output/levels2009, ci bdec(3) ctitle("`x'", "non-blacks", "3 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 577 & x_c !=24 , r
	outreg2 using output/levels2009, ci bdec(3) ctitle("`x'", "non-blacks", "2 yrs")append excel

	regress `x' i.bmonth, robust 
	predict y`x', xb
	gen e`x'=`x'-y`x'
	
	rdrobust e`x' x_c, bwselect(IK)
	outreg2 using output/levels2009, ci bdec(3)  ctitle("`x'", "non-blacks", "IK") append excel
	rdrobust e`x' x_c, bwselect(CCT)
	outreg2 using output/levels2009, ci bdec(3)  ctitle("`x'", "non-blacks", "CCT") append excel
	drop y`x' e`x'
}
*Appendix Table 23, nonblacks
foreach x of var $costs {
	reg `x' post post#(c.x_c c.x_c2) i.bmonth, r
	outreg2 using output/costs, ci bdec(3) ctitle("`x'", "non-blacks", "4 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 1297 & x_c !=36, r
	outreg2 using output/costs, ci bdec(3) ctitle("`x'", "non-blacks", "3 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 577 & x_c !=24 , r
	outreg2 using output/costs, ci bdec(3) ctitle("`x'", "non-blacks", "2 yrs")append excel

	regress `x' i.bmonth, robust 
	predict y`x', xb
	gen e`x'=`x'-y`x'
		
	rdrobust e`x' x_c, bwselect(IK)
	outreg2 using output/costs, ci bdec(3)  ctitle("`x'", "non-blacks", "IK") append excel
	rdrobust e`x' x_c, bwselect(CCT)
	outreg2 using output/costs, ci bdec(3)  ctitle("`x'", "non-blacks", "CCT") append excel
	drop y`x' e`x'
}

/************************************************************
D) 2009 Hospital data, low-income, all races
*************************************************************/
clear
insheet using src/allracesli2009.csv

sort byear bmonth
egen x_c=group(byear bmonth)
replace x_c=x_c-49
gen post=1*(x_c>=0)
gen x_c2=x_c^2

**chronic/nonchronic
gen logchron=log(hcupchronic)
gen lognonchron=log(count-preg-hcupchronic)

gen nonchron=count-preg-hcupchronic
gen nonpreg=count-preg

**for placebo tests
gen loga=log(appendicitis)
gen logi=log(injury)


***all non pregnancy hospitalizations + alternative specifications
global main lognp logchron lognonchron loga logi 
global levs nonpreg hcupchronic nonchron

!rm output/main2009li*
!rm output/levels2009li*

*Appendix Tables 3 and 21, all races
foreach x of var $main {
	reg `x' post post#(c.x_c c.x_c2) i.bmonth, r
	outreg2 using output/main2009li, ci bdec(3) ctitle("`x'", "all races", "4 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 1297 & x_c !=36, r
	outreg2 using output/main2009li, ci bdec(3) ctitle("`x'", "all races", "3 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 577 & x_c !=24 , r
	outreg2 using output/main2009li, ci bdec(3) ctitle("`x'", "all races", "2 yrs")append excel

	regress `x' i.bmonth, robust 
	predict y`x', xb
	gen e`x'=`x'-y`x'
		
	rdrobust e`x' x_c, bwselect(IK)
	outreg2 using output/main2009li, ci bdec(3)  ctitle("`x'", "all races", "IK") append excel
	rdrobust e`x' x_c, bwselect(CCT)
	outreg2 using output/main2009li, ci bdec(3)  ctitle("`x'", "all races", "CCT") append excel
	drop y`x' e`x'
}

*Appendix Table 25
foreach x of var $levs {
	reg `x' post post#(c.x_c c.x_c2) i.bmonth, r
	outreg2 using output/levels2009li, ci bdec(3) ctitle("`x'", "all races", "4 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 1297 & x_c !=36, r
	outreg2 using output/levels2009li, ci bdec(3) ctitle("`x'", "all races", "3 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 577 & x_c !=24 , r
	outreg2 using output/levels2009li, ci bdec(3) ctitle("`x'", "all races", "2 yrs")append excel

	regress `x' i.bmonth, robust 
	predict y`x', xb
	gen e`x'=`x'-y`x'
		
	rdrobust e`x' x_c, bwselect(IK)
	outreg2 using output/levels2009li, ci bdec(3)  ctitle("`x'", "all races", "IK") append excel
	rdrobust e`x' x_c, bwselect(CCT)
	outreg2 using output/levels2009li, ci bdec(3)  ctitle("`x'", "all races", "CCT") append excel
	drop y`x' e`x'
}

/************************************************************
E) 2009 Hospital data, low-income, blacks
*************************************************************/
clear
insheet using src/blackli2009.csv

sort byear bmonth
egen x_c=group(byear bmonth)
replace x_c=x_c-49
gen post=1*(x_c>=0)
gen x_c2=x_c^2

**chronic/nonchronic
gen logchron=log(hcupchronic)
gen lognonchron=log(count-preg-hcupchronic)

gen nonchron=count-preg-hcupchronic
gen nonpreg=count-preg

**for placebo tests
gen loga=log(appendicitis)
gen logi=log(injury)

***all non pregnancy hospitalizations + alternative specifications
global main lognp logchron lognonchron loga logi 
global levs nonpreg hcupchronic nonchron

*Table 6 and Appendix Table 21, blacks
foreach x of var $main {
	reg `x' post post#(c.x_c c.x_c2) i.bmonth, r
	outreg2 using output/main2009li, ci bdec(3) ctitle("`x'", "blacks", "4 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 1297 & x_c !=36, r
	outreg2 using output/main2009li, ci bdec(3) ctitle("`x'", "blacks", "3 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 577 & x_c !=24 , r
	outreg2 using output/main2009li, ci bdec(3) ctitle("`x'", "blacks", "2 yrs")append excel

	regress `x' i.bmonth, robust 
	predict y`x', xb
	gen e`x'=`x'-y`x'
		
	rdrobust e`x' x_c, bwselect(IK)
	outreg2 using output/main2009li, ci bdec(3)  ctitle("`x'", "blacks", "IK") append excel
	rdrobust e`x' x_c, bwselect(CCT)
	outreg2 using output/main2009li, ci bdec(3)  ctitle("`x'", "blacks", "CCT") append excel
	drop y`x' e`x'
}

*Appendix Table 25
foreach x of var $levs {
	reg `x' post post#(c.x_c c.x_c2) i.bmonth, r
	outreg2 using output/levels2009li, ci bdec(3) ctitle("`x'", "blacks", "4 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 1297 & x_c !=36, r
	outreg2 using output/levels2009li, ci bdec(3) ctitle("`x'", "blacks", "3 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 577 & x_c !=24 , r
	outreg2 using output/levels2009li, ci bdec(3) ctitle("`x'", "blacks", "2 yrs")append excel

	regress `x' i.bmonth, robust 
	predict y`x', xb
	gen e`x'=`x'-y`x'
		
	rdrobust e`x' x_c, bwselect(IK)
	outreg2 using output/levels2009li, ci bdec(3)  ctitle("`x'", "blacks", "IK") append excel
	rdrobust e`x' x_c, bwselect(CCT)
	outreg2 using output/levels2009li, ci bdec(3)  ctitle("`x'", "blacks", "CCT") append excel
	drop y`x' e`x'
}

/************************************************************
F) 2009 Hospital data, low-income, nonblacks
*************************************************************/

clear
insheet using src/nonblackli2009.csv

sort byear bmonth
egen x_c=group(byear bmonth)
replace x_c=x_c-49
gen post=1*(x_c>=0)
gen x_c2=x_c^2

**chronic/nonchronic
gen logchron=log(hcupchronic)
gen lognonchron=log(count-preg-hcupchronic)

gen nonchron=count-preg-hcupchronic
gen nonpreg=count-preg

**for placebo tests
gen loga=log(appendicitis)
gen logi=log(injury)

***all non pregnancy hospitalizations + alternative specifications
global main lognp logchron lognonchron loga logi 
global levs nonpreg hcupchronic nonchron

*Table 6 and Appendix Table 21, nonblacks
foreach x of var $main {
	reg `x' post post#(c.x_c c.x_c2) i.bmonth, r
	outreg2 using output/main2009li, ci bdec(3) ctitle("`x'", "non-blacks", "4 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 1297 & x_c !=36, r
	outreg2 using output/main2009li, ci bdec(3) ctitle("`x'", "non-blacks", "3 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 577 & x_c !=24 , r
	outreg2 using output/main2009li, ci bdec(3) ctitle("`x'", "non-blacks", "2 yrs")append excel

	regress `x' i.bmonth, robust 
	predict y`x', xb
	gen e`x'=`x'-y`x'
		
	rdrobust e`x' x_c, bwselect(IK)
	outreg2 using output/main2009li, ci bdec(3)  ctitle("`x'", "non-blacks", "IK") append excel
	rdrobust e`x' x_c, bwselect(CCT)
	outreg2 using output/main2009li, ci bdec(3)  ctitle("`x'", "non-blacks", "CCT") append excel
	drop y`x' e`x'
}

*Appendix Table 25
foreach x of var $levs {
	reg `x' post post#(c.x_c c.x_c2) i.bmonth, r
	outreg2 using output/levels2009li, ci bdec(3) ctitle("`x'", "non-blacks", "4 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 1297 & x_c !=36, r
	outreg2 using output/levels2009li, ci bdec(3) ctitle("`x'", "non-blacks", "3 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 577 & x_c !=24 , r
	outreg2 using output/levels2009li, ci bdec(3) ctitle("`x'", "non-blacks", "2 yrs")append excel

	regress `x' i.bmonth, robust 
	predict y`x', xb
	gen e`x'=`x'-y`x'
		
	rdrobust e`x' x_c, bwselect(IK)
	outreg2 using output/levels2009li, ci bdec(3)  ctitle("`x'", "non-blacks", "IK") append excel
	rdrobust e`x' x_c, bwselect(CCT)
	outreg2 using output/levels2009li, ci bdec(3)  ctitle("`x'", "non-blacks", "CCT") append excel
	drop y`x' e`x'
}

/************************************************************
G) 2009 ED data, all races
*************************************************************/
clear
insheet using src/Ed.csv

sort byear bmonth
egen x_c=group(byear bmonth)
replace x_c=x_c-49
gen post=1*(x_c>=0)
gen x_c2=x_c^2

gen lognp=log(count-preg)

**chronic/nonchronic
gen logchron=log(hcupchronic)
gen lognonchron=log(count-preg-hcupchronic)

gen logcost=log(cost)

***all non pregnancy hospitalizations + alternative specifications
global main lognp logchron lognonchron logcost

!rm output/ed2009*

*Table 5 and Appendix Table 23
foreach x of var $main {
	reg `x' post post#(c.x_c c.x_c2) i.bmonth, r
	outreg2 using output/ed2009, ci bdec(3) ctitle("`x'", "all races", "4 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 1297 & x_c !=36, r
	outreg2 using output/ed2009, ci bdec(3) ctitle("`x'", "all races", "3 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 577 & x_c !=24 , r
	outreg2 using output/ed2009, ci bdec(3) ctitle("`x'", "all races", "2 yrs")append excel

	regress `x' i.bmonth, robust 
	predict y`x', xb
	gen e`x'=`x'-y`x'
		
	rdrobust e`x' x_c, bwselect(IK)
	outreg2 using output/ed2009, ci bdec(3)  ctitle("`x'", "all races", "IK") append excel
	rdrobust e`x' x_c, bwselect(CCT)
	outreg2 using output/ed2009, ci bdec(3)  ctitle("`x'", "all races", "CCT") append excel
	drop y`x' e`x'
}

/************************************************************
H) 2009 ED data, blacks
*************************************************************/
clear
insheet using src/Edblack.csv

sort byear bmonth
egen x_c=group(byear bmonth)
replace x_c=x_c-49
gen post=1*(x_c>=0)
gen x_c2=x_c^2

gen lognp=log(count-preg)

**chronic/nonchronic
gen logchron=log(hcupchronic)
gen lognonchron=log(count-preg-hcupchronic)

gen logcost=log(cost)
gen logpubc=log(publicc)
gen logselfc=log(selfc)
gen logprivc=log(privatec)
gen logfreec=log(freecarec)

**By DX code
gen loginfect=log(infect)
gen logneoplams=log(neoplasms)
gen logendometa=log(endometa)
gen logblood=log(blood)
gen logmentaldisorders=log(mentaldisorders)
gen lognervoussys=log(nervoussys)
gen logsense=log(sense)
gen logcirculatory=log(circulatory)
gen logrespiratory=log(respiratory)
gen logdigestive=log(digestive)
gen loggenitourinary=log(genitourinary)
gen logskin=log(skin)
gen logmuscle=log(muscle)
gen logcongenital=log(congenital)
gen logilldefined=log(illdefined)

summ infect neoplasms endometa blood mentaldisorders nervoussys sense ///
circulatory respiratory digestive genitourinary skin muscle congenital illdefined

***all non pregnancy hospitalizations + alternative specifications
global main lognp logchron lognonchron logcost logpubc logselfc logprivc logfreec
global outcomes loginfect logneoplams logendometa logblood logmentaldisorders ///
lognervoussys logsense logcirculatory logrespiratory logdigestive ///
loggenitourinary logskin logmuscle logcongenital logilldefined

*Table 5, Appendix Tables 23-24
foreach x of var $main {
	reg `x' post post#(c.x_c c.x_c2) i.bmonth, r
	outreg2 using output/ed2009, ci bdec(3) ctitle("`x'", "blacks", "4 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 1297 & x_c !=36, r
	outreg2 using output/ed2009, ci bdec(3) ctitle("`x'", "blacks", "3 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 577 & x_c !=24 , r
	outreg2 using output/ed2009, ci bdec(3) ctitle("`x'", "blacks", "2 yrs")append excel

	regress `x' i.bmonth, robust 
	predict y`x', xb
	gen e`x'=`x'-y`x'
		
	rdrobust e`x' x_c, bwselect(IK)
	outreg2 using output/ed2009, ci bdec(3)  ctitle("`x'", "blacks", "IK") append excel
	rdrobust e`x' x_c, bwselect(CCT)
	outreg2 using output/ed2009, ci bdec(3)  ctitle("`x'", "blacks", "CCT") append excel
	drop y`x' e`x'
}

!rm output/diagcodesed2009*
*Appendix Table 7
foreach x of var $outcomes {
	reg `x' post post#(c.x_c c.x_c2) i.bmonth, r
	outreg2 using output/diagcodesed2009, ci bdec(3) ctitle("`x'", "blacks", "4 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 1297 & x_c !=36, r
	outreg2 using output/diagcodesed2009, ci bdec(3) ctitle("`x'", "blacks", "3 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 577 & x_c !=24 , r
	outreg2 using output/diagcodesed2009, ci bdec(3) ctitle("`x'", "blacks", "2 yrs")append excel

	regress `x' i.bmonth, robust 
	predict y`x', xb
	gen e`x'=`x'-y`x'
		
	rdrobust e`x' x_c, bwselect(IK)
	outreg2 using output/diagcodesed2009, ci bdec(3)  ctitle("`x'", "blacks", "IK") append excel
	rdrobust e`x' x_c, bwselect(CCT)
	outreg2 using output/diagcodesed2009, ci bdec(3)  ctitle("`x'", "blacks", "CCT") append excel
	drop y`x' e`x'
}

/************************************************************
I) 2009 ED data, nonblacks
*************************************************************/
clear
insheet using src/nonblacked.csv

sort byear bmonth
egen x_c=group(byear bmonth)
replace x_c=x_c-49
gen post=1*(x_c>=0)
gen x_c2=x_c^2

gen lognp=log(count-preg)

**chronic/nonchronic
gen logchron=log(hcupchronic)
gen lognonchron=log(count-preg-hcupchronic)

gen logcost=log(cost)

***all non pregnancy hospitalizations + alternative specifications
global main lognp logchron lognonchron logcost 

*Table 5 and Appendix Table 23
foreach x of var $main {
	reg `x' post post#(c.x_c c.x_c2) i.bmonth, r
	outreg2 using output/ed2009, ci bdec(3) ctitle("`x'", "non-blacks", "4 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 1297 & x_c !=36, r
	outreg2 using output/ed2009, ci bdec(3) ctitle("`x'", "non-blacks", "3 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 577 & x_c !=24 , r
	outreg2 using output/ed2009, ci bdec(3) ctitle("`x'", "non-blacks", "2 yrs")append excel

	regress `x' i.bmonth, robust 
	predict y`x', xb
	gen e`x'=`x'-y`x'
		
	rdrobust e`x' x_c, bwselect(IK)
	outreg2 using output/ed2009, ci bdec(3)  ctitle("`x'", "non-blacks", "IK") append excel
	rdrobust e`x' x_c, bwselect(CCT)
	outreg2 using output/ed2009, ci bdec(3)  ctitle("`x'", "non-blacks", "CCT") append excel
	drop y`x' e`x'
}

/************************************************************
J) 2009 ED data, low-income, all races
*************************************************************/
clear
insheet using src/allracesedli.csv

sort byear bmonth
egen x_c=group(byear bmonth)
replace x_c=x_c-49
gen post=1*(x_c>=0)
gen x_c2=x_c^2

gen lognp=log(count)

**chronic/nonchronic
gen logchron=log(hcupchronic)
gen lognonchron=log(count-hcupchronic)


***all non pregnancy hospitalizations + alternative specifications
global main lognp logchron lognonchron 

!rm output/ed2009li*

*Appendix Table 3, all races
foreach x of var $main {
	reg `x' post post#(c.x_c c.x_c2) i.bmonth, r
	outreg2 using output/ed2009li, ci bdec(3) ctitle("`x'", "all races", "4 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 1297 & x_c !=36, r
	outreg2 using output/ed2009li, ci bdec(3) ctitle("`x'", "all races", "3 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 577 & x_c !=24 , r
	outreg2 using output/ed2009li, ci bdec(3) ctitle("`x'", "all races", "2 yrs")append excel

	regress `x' i.bmonth, robust 
	predict y`x', xb
	gen e`x'=`x'-y`x'
		
	rdrobust e`x' x_c, bwselect(IK)
	outreg2 using output/ed2009li, ci bdec(3)  ctitle("`x'", "all races", "IK") append excel
	rdrobust e`x' x_c, bwselect(CCT)
	outreg2 using output/ed2009li, ci bdec(3)  ctitle("`x'", "all races", "CCT") append excel
	drop y`x' e`x'
}

/************************************************************
K) 2009 ED data, low-income, black
*************************************************************/
clear
insheet using src/blackedli.csv

sort byear bmonth
egen x_c=group(byear bmonth)
replace x_c=x_c-49
gen post=1*(x_c>=0)
gen x_c2=x_c^2

gen lognp=log(count)

**chronic/nonchronic
gen logchron=log(hcupchronic)
gen lognonchron=log(count-hcupchronic)

***all non pregnancy hospitalizations + alternative specifications
global main lognp logchron lognonchron 

*Table 7, blacks
foreach x of var $main {
	reg `x' post post#(c.x_c c.x_c2) i.bmonth, r
	outreg2 using output/ed2009li, ci bdec(3) ctitle("`x'", "blacks", "4 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 1297 & x_c !=36, r
	outreg2 using output/ed2009li, ci bdec(3) ctitle("`x'", "blacks", "3 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 577 & x_c !=24 , r
	outreg2 using output/ed2009li, ci bdec(3) ctitle("`x'", "blacks", "2 yrs")append excel

	regress `x' i.bmonth, robust 
	predict y`x', xb
	gen e`x'=`x'-y`x'
		
	rdrobust e`x' x_c, bwselect(IK)
	outreg2 using output/ed2009li, ci bdec(3)  ctitle("`x'", "blacks", "IK") append excel
	rdrobust e`x' x_c, bwselect(CCT)
	outreg2 using output/ed2009li, ci bdec(3)  ctitle("`x'", "blacks", "CCT") append excel
	drop y`x' e`x'
}

/************************************************************
L) 2009 ED data, low-income, nonblack
*************************************************************/
clear
insheet using src/nonblackedli.csv

sort byear bmonth
egen x_c=group(byear bmonth)
replace x_c=x_c-49
gen post=1*(x_c>=0)
gen x_c2=x_c^2

gen lognp=log(count)

**chronic/nonchronic
gen logchron=log(hcupchronic)
gen lognonchron=log(count-hcupchronic)

global main lognp logchron lognonchron 
*Table 7, nonblacks
foreach x of var $main {
	reg `x' post post#(c.x_c c.x_c2) i.bmonth, r
	outreg2 using output/ed2009li, ci bdec(3) ctitle("`x'", "non-blacks", "4 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 1297 & x_c !=36, r
	outreg2 using output/ed2009li, ci bdec(3) ctitle("`x'", "non-blacks", "3 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 577 & x_c !=24 , r
	outreg2 using output/ed2009li, ci bdec(3) ctitle("`x'", "non-blacks", "2 yrs")append excel

	regress `x' i.bmonth, robust 
	predict y`x', xb
	gen e`x'=`x'-y`x'
		
	rdrobust e`x' x_c, bwselect(IK)
	outreg2 using output/ed2009li, ci bdec(3)  ctitle("`x'", "non-blacks", "IK") append excel
	rdrobust e`x' x_c, bwselect(CCT)
	outreg2 using output/ed2009li, ci bdec(3)  ctitle("`x'", "non-blacks", "CCT") append excel
	drop y`x' e`x'
}


/************************************************************
M) 1999 hospital data, all races
*************************************************************/
clear
insheet using src/allraces99.csv

sort byear bmonth
egen x_c=group(byear bmonth)
replace x_c=x_c-49
gen post=1*(x_c>=0)
gen x_c2=x_c^2

**chronic/nonchronic
gen logchron=log(hcupchronic)
gen lognonchron=log(count-preg-hcupchronic)

global main lognp logchron lognonchron 

*Appendix Table 3, all races
foreach x of var $main {
	reg `x' post post#(c.x_c c.x_c2) i.bmonth, r
	outreg2 using output/main1999, ci bdec(3) ctitle("`x'", "all races", "4 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 1297 & x_c !=36, r
	outreg2 using output/main1999, ci bdec(3) ctitle("`x'", "all races", "3 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 577 & x_c !=24 , r
	outreg2 using output/main1999, ci bdec(3) ctitle("`x'", "all races", "2 yrs")append excel

	regress `x' i.bmonth, robust 
	predict y`x', xb
	gen e`x'=`x'-y`x'
	
	rdrobust e`x' x_c, bwselect(IK)
	outreg2 using output/main1999, ci bdec(3)  ctitle("`x'", "all races", "IK") append excel
	rdrobust e`x' x_c, bwselect(CCT)
	outreg2 using output/main1999, ci bdec(3)  ctitle("`x'", "all races", "CCT") append excel
	drop y`x' e`x'
}

/************************************************************
N) 1999 hospital data, blacks
*************************************************************/
clear
insheet using src/black99.csv

sort byear bmonth
egen x_c=group(byear bmonth)
replace x_c=x_c-49
gen post=1*(x_c>=0)
gen x_c2=x_c^2

**chronic/nonchronic
gen logchron=log(hcupchronic)
gen lognonchron=log(count-preg-hcupchronic)

*Table 3, blacks
foreach x of var $main {
	reg `x' post post#(c.x_c c.x_c2) i.bmonth, r
	outreg2 using output/main1999, ci bdec(3) ctitle("`x'", "blacks", "4 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 1297 & x_c !=36, r
	outreg2 using output/main1999, ci bdec(3) ctitle("`x'", "blacks", "3 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 577 & x_c !=24 , r
	outreg2 using output/main1999, ci bdec(3) ctitle("`x'", "blacks", "2 yrs")append excel

	regress `x' i.bmonth, robust 
	predict y`x', xb
	gen e`x'=`x'-y`x'
	
	rdrobust e`x' x_c, bwselect(IK)
	outreg2 using output/main1999, ci bdec(3)  ctitle("`x'", "blacks", "IK") append excel
	rdrobust e`x' x_c, bwselect(CCT)
	outreg2 using output/main1999, ci bdec(3)  ctitle("`x'", "blacks", "CCT") append excel
	drop y`x' e`x'
}

/************************************************************
O) 1999 hospital data, nonblacks
*************************************************************/
clear
insheet using src/notblack1999.csv

sort byear bmonth
egen x_c=group(byear bmonth)
replace x_c=x_c-49
gen post=1*(x_c>=0)
gen x_c2=x_c^2

**chronic/nonchronic
gen logchron=log(hcupchronic)
gen lognonchron=log(count-preg-hcupchronic)

*Table 3, nonblacks
foreach x of var $main {
	reg `x' post post#(c.x_c c.x_c2) i.bmonth, r
	outreg2 using output/main1999, ci bdec(3) ctitle("`x'", "non-blacks", "4 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 1297 & x_c !=36, r
	outreg2 using output/main1999, ci bdec(3) ctitle("`x'", "non-blacks", "3 yrs")append excel
	reg `x' post post#(c.x_c c.x_c2) i.bmonth if x_c2 < 577 & x_c !=24 , r
	outreg2 using output/main1999, ci bdec(3) ctitle("`x'", "non-blacks", "2 yrs")append excel

	regress `x' i.bmonth, robust 
	predict y`x', xb
	gen e`x'=`x'-y`x'
		
	rdrobust e`x' x_c, bwselect(IK)
	outreg2 using output/main1999, ci bdec(3)  ctitle("`x'", "non-blacks", "IK") append excel
	rdrobust e`x' x_c, bwselect(CCT)
	outreg2 using output/main1999, ci bdec(3)  ctitle("`x'", "non-blacks", "CCT") append excel
	drop y`x' e`x'
}

